import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class DictService extends BaseService<any> {
  private readonly logger = new Logger(DictService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: DictService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id,name,key,memo,create_time,update_time';         $order = 'id desc';          $search_model = $this->model->withSearch(["name","key"], $wh...
const field = 'id,name,key,memo,create_time,update_time';
        const order = 'id desc';

        const search_model = this.model.withSearch(["name","key"), where).field(field).order(order];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * 获取数据字典信息
     * @param int id
     * @return */
    async getInfo(int id)
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['id', '=', id))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * getInfo
   * 对应 PHP: DictService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id,name,key,dictionary,memo,create_time,update_time';          $info = $this->model->field($field)->where([['id', '=', $id]])->findOrEmpty()...
const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['id', '=', id))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * add
   * 对应 PHP: DictService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $res = $this->model->create($data);         return $res->id;      }      /**      * 数据字典编辑      * @param int $id      * @param array $data      * @ret...
const res = this.model.create(data);
        return res.id;

    }

    /**
     * 数据字典编辑
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        data.update_time = time(];
        this.model.where([['id', '=', id))).update(data);
        return true;
    }

    /**
     * 删除数据字典
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        const res = this.model.where([['id', '=', id))).delete(];
        return res;
    }

    /**
     * 获取全部数据字典
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAll()
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';
        const list = this.model.field(field).select().toArray(];
        return list;
    }

    async getKeyInfo(key)
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['key', '=', key))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * edit
   * 对应 PHP: DictService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $data['update_time'] = time();         $this->model->where([['id', '=', $id]])->update($data);         return true;     }      /**      * 删除数据字典      ...
data.update_time = time(];
        this.model.where([['id', '=', id))).update(data);
        return true;
    }

    /**
     * 删除数据字典
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        const res = this.model.where([['id', '=', id))).delete(];
        return res;
    }

    /**
     * 获取全部数据字典
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAll()
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';
        const list = this.model.field(field).select().toArray(];
        return list;
    }

    async getKeyInfo(key)
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['key', '=', key))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * del
   * 对应 PHP: DictService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: $res = $this->model->where([['id', '=', $id]])->delete();         return $res;     }      /**      * 获取全部数据字典      * @return array      * @throws Data...
const res = this.model.where([['id', '=', id))).delete(];
        return res;
    }

    /**
     * 获取全部数据字典
     * @return * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async getAll()
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';
        const list = this.model.field(field).select().toArray(];
        return list;
    }

    async getKeyInfo(key)
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['key', '=', key))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * getAll
   * 对应 PHP: DictService_admin::getAll()
   * 逻辑类型: undefined - undefined
   */
  async getAll() {
    // 基于PHP真实逻辑: getAll
    // PHP原文: $field = 'id,name,key,dictionary,memo,create_time,update_time';         $list = $this->model->field($field)->select()->toArray();         return $list...
const field = 'id,name,key,dictionary,memo,create_time,update_time';
        const list = this.model.field(field).select().toArray(];
        return list;
    }

    async getKeyInfo(key)
    {
        const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['key', '=', key))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }

  /**
   * getKeyInfo
   * 对应 PHP: DictService_admin::getKeyInfo()
   * 逻辑类型: undefined - undefined
   */
  async getKeyInfo(key: any) {
    // 基于PHP真实逻辑: getKeyInfo
    // PHP原文: $field = 'id,name,key,dictionary,memo,create_time,update_time';          $info = $this->model->field($field)->where([['key', '=', $key]])->findOrEmpty...
const field = 'id,name,key,dictionary,memo,create_time,update_time';

        const info = this.model.field(field).where([['key', '=', key))).findOrEmpty().toArray(];
        if (info.dictionary === null) {
            info.dictionary = [];
  }
}
